import os
import random
import re
import time
 

def is_file_empty(file_path):
    return os.path.getsize(file_path) == 0
#MARK: 这里是保存数据到文件
def save_FileIo(uuid):
        a = uuid
        try:
           with open('exampleString.txt', 'a') as file:
               file_path = 'exampleString.txt'
               if is_file_empty(file_path):
                     print("文件为空")
                     file.write(a)
               else:
                     print("文件不为空")
                     file.write('\n'+ a)
               
        except FileNotFoundError:
           with open('exampleString.txt', 'w') as file:
               file_path = 'exampleString.txt'
               if is_file_empty(file_path):
                     print("文件为空")
                     file.write(a)
               else:
                     print("文件不为空")
                     file.write('\n'+ a)



#MARK: 这里是获取数据到文件
def get_FileIo():
    aryStr = []
    s = ""
    with open('exampleString.txt', 'r') as file:
         for line in file:
             line = line.strip()
             if line and line[0] != '':  # 去除行尾的换行符
                strName = line
                aryStr.append(strName)
                s = aryStr[0]
                print(aryStr)
    if aryStr.count != 0 and len(aryStr)!= 0:
        aryStr.remove(s)
       
        for i in aryStr:
             if i == aryStr[0]:
                   with open('exampleString.txt', 'w') as file:
                        file_path = 'exampleString.txt'
                        if is_file_empty(file_path):
                         print("文件为空")
                         file.write(i)
                        else:
                         print("文件不为空")
                         file.write('\n'+ i)
             else:
                  with open('exampleString.txt', 'a') as file:
                        file_path = 'exampleString.txt'
                        if is_file_empty(file_path):
                         print("文件为空")
                         file.write(i)
                        else:
                         print("文件不为空")
                         file.write('\n'+ i)
        
        if len(aryStr)== 0:
             print("文件为空")
             with open('exampleString.txt', 'w') as file:
                file_path = 'exampleString.txt'
                if is_file_empty(file_path):
                     print("文件为空")
                     file.write("")
                else:
                     print("文件不为空")
                     file.write("")
        return s
    return "没有找到"

#MARK: 这里是删除文件中的内容
def delete_text(file_path, target_text):
    with open(file_path, 'r+') as f:
        content = f.read()
        modified_content = content.replace(target_text, '\b')  # 替换为空字符串[1,2](@ref)
        f.seek(0)  # 重置指针到文件开头
        f.write(modified_content)
        f.truncate()